package com.etrans.web.gps.business.base.controller;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.base.domain.VehicleVo;
import com.etrans.web.gps.business.base.service.VehicleBaseService;
import com.etrans.web.gps.common.Constant;
import com.etrans.web.gps.common.page.PageResultVo;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.common.page.ResultContans;
import com.etrans.web.gps.util.CsEncodeUtils;
import com.etrans.web.gps.util.DateUtil;
import com.etrans.web.gps.util.grid.FlexiGridUtil;
import com.etrans.web.gps.util.grid.ParamMap;
import com.etrans.web.gps.util.grid.ParamValueObject;






/**
 * 维护车辆信息控制层
 * @author tanhang
 *
 */
@Controller
@RequestMapping(value="/base/vehicle/*")
public class VehicleBaseController extends BaseController {
	
	private final static Logger logger = LoggerFactory.getLogger("VehicleBaseController");
	
	@Autowired
	private VehicleBaseService vehicleBaseService;
	
	
	
	/**
	 * 进入车辆管理页面
	 * @param req
	 * @param res
	 * @param model
	 * @param dbKey
	 * @return
	 */
	@RequestMapping(value="vehicle.html")
	public  String getIntoVehicle(){
		
		return "views/base/vehicle/vehicle";
	}
	
	/**
	 * 查询用户下的全部车辆
	 */
	@RequestMapping(value="loadVehicle.html")
	@ResponseBody
	public  PageResultVo loadVehicle(String paramsGrid){
		PageResultVo pageResultVo = new PageResultVo();
		ParamMap param = FlexiGridUtil.parseJSONParam(paramsGrid);
		ParamValueObject inputParam = this.parseJSONParam(param);
		
		//SysUser user = (SysUser)getSession().getAttribute(Constant.LOGIN_SESSION_NAME);
		
		String userId="1214";//String.valueOf(user.getId());
		
		
		param.getWhereParam().put("userId",userId);
		
		List<VehicleVo> vehicleList=vehicleBaseService.queryVehicle( param.getWhereParam());
		
		PageVo pageBean = new PageVo();
		pageBean.setRows(vehicleList);
		pageBean.setPage(inputParam.getFromNumber());
		pageBean.setTotal(vehicleList.size());
		pageResultVo.setData(pageBean);
		pageResultVo.setCode(ResultContans.RETURN_OK_CODE.getCode());
		pageResultVo.setMsg(ResultContans.RETURN_OK_CODE.getName());
		
		return pageResultVo;
	}
	
	/**
	 * 分享车辆
	 */
	@RequestMapping(value="shareVehicle.html")
	public String shareVehicle(String vehicleId,Model model){
		
		HashMap<String, Object> param=getReqParameters();
		List<HashMap<String, Object>> list=new ArrayList<>(); 
		String url="views/base/vehicle/shareVehicleOverdue";
		try{
		
			Date nowDate=new Date();
			param.put("vehicleId",vehicleId);
			String userId=(String)param.get("userId");
			if(userId==null||"".equals("userId")){ 
				//SysUser user = (SysUser)getSession().getAttribute(Constant.LOGIN_SESSION_NAME);			
				userId="1214";//String.valueOf(user.getId());				
			}
			
			param.put("userId",userId);
			
			param.put("vehicleId",String.valueOf(CsEncodeUtils.Decrypt2H(vehicleId,Constant.VEHICLE_PASS_KEY)));
			
			List<VehicleVo> vehicleList=vehicleBaseService.queryVehicle( param);
			
			if(vehicleList!=null&&vehicleList.size()==1){
				VehicleVo obj=vehicleList.get(0);
				String shareKey=obj.getShareKey();
				
				if(shareKey==null||"".equals(shareKey)){
					return url;
				}
				
				Date keyDate=DateUtil.getStrToDateFormat(shareKey,null);
				if(keyDate.getTime()>nowDate.getTime()){
					url="views/base/vehicle/vehicleMonitor";
				}
				
				model.addAttribute("vehicleID",obj.getVehicleId());
				model.addAttribute("commNO",obj.getCommNo());
				model.addAttribute("shareKey",obj.getShareKey());
				model.addAttribute("RegistrationNO",obj.getRegistrationNO());
			}
			
			
				
		}catch (Exception e) {
			e.printStackTrace();
			logger.info("分享车辆错误：{}",e);
		}
		
		return url;
		
	}
}
